
class Main extends eui.UILayer {
    /**
     * 加载进度界面
     * loading process interface
     */
    private loadingView: LoadingUI;
    protected createChildren(): void {
        //inject the custom material parser
        //注入自定义的素材解析器
        var assetAdapter = new AssetAdapter();
        this.stage.registerImplementation("eui.IAssetAdapter", assetAdapter);
        this.stage.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
        //Config loading process interface
        this.stage.addEventListener(egret.Event.RESIZE, () => {
            threeJsScreenChange();
            //刷新一下
            if (GameTools.refresh_mPr != null)
                GameTools.refresh_mPr();
        }, this);
        //设置加载进度界面
        this.loadingView = new LoadingUI();
        this.stage.addChild(this.loadingView);
        // initialize the Resource loading library
        //初始化Resource资源加载库
        RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        RES.loadConfig("resource/default.res.json", "resource/");
    }
    /**
     * 配置文件加载完成,开始预加载皮肤主题资源和preload资源组。
     * Loading of configuration file is complete, start to pre-load the theme configuration file and the preload resource group
     */
    private onConfigComplete(event: RES.ResourceEvent): void {
        RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigComplete, this);
        // load skin theme configuration file, you can manually modify the file. And replace the default skin.
        //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
        var theme = new eui.Theme("resource/default.thm.json", this.stage);
        theme.addEventListener(eui.UIEvent.COMPLETE, this.onThemeLoadComplete, this);

        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
        RES.loadGroup("preload");
    }
    private isThemeLoadEnd: boolean = false;
    /**
     * 主题文件加载完成,开始预加载
     * Loading of theme configuration file is complete, start to pre-load the 
     */
    private onThemeLoadComplete(): void {
        this.isThemeLoadEnd = true;
        this.changeScreenSize(720, 1280);
        this.mPR();
        three_Rate = _rate;
        GameTools.refresh_mPr = () => { this.mPR(); };

        this.createScene();
    }
    private isResourceLoadEnd: boolean = false;
    /**
     * preload资源组加载完成
     * preload resource group is loaded
     */
    private onResourceLoadComplete(event: RES.ResourceEvent): void {
        if (event.groupName == "preload") {
            this.stage.removeChild(this.loadingView);
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            this.isResourceLoadEnd = true;
            this.createScene();
        }
    }
    private createScene() {
        if (this.isThemeLoadEnd && this.isResourceLoadEnd) {
            this.startCreateScene();
        }
    }
    /**
     * 资源组加载出错
     * Resource group loading failed
     */
    private onResourceLoadError(event: RES.ResourceEvent): void {
        //TODO
        console.warn("Group:" + event.groupName + " has failed to load");
        //忽略加载失败的项目
        //ignore loading failed projects
        this.onResourceLoadComplete(event);
    }
    /**
     * preload资源组加载进度
     * loading process of preload resource
     */
    private onResourceProgress(event: RES.ResourceEvent): void {
        if (event.groupName == "preload") {
            this.loadingView.setProgress(event.itemsLoaded, event.itemsTotal);
        }
    }
    /**
     * 创建场景界面
     * Create scene interface
     */
    protected startCreateScene(): void {
        GameData.isScoll = false;
        GameData.stage = this.stage;

        add3DBall();

        var tableUI: TableUI = TableUI.Instance();
        this.addChild(tableUI);

        //var timer = new egret.Timer(300,0);
        //timer.addEventListener(egret.TimerEvent.TIMER,this.getScene,this);
        //timer.start();

        window.addEventListener('resize', this.getScene);
    }

    private getScene() {
        IsDifferent();
    }

    private mPR() {
        var rateW = screenSize.width / document.body.clientWidth;
        var rateH = screenSize.height / document.body.clientHeight;
        if (rateW >= rateH) {
            bep = 0;
            _rate = rateW;
            bel = (document.body.clientHeight - screenSize.height / rateW) / 2;
        }
        else {
            bep = 1;
            _rate = rateH;
            bel = (document.body.clientWidth - screenSize.width / rateH) / 2;
        }
    }


    private changeScreenSize(width, height) {
        screenSize.width = width;
        screenSize.height = height;
        egret.MainContext.instance.stage.setContentSize(screenSize.width, screenSize.height);
        if (isRotation)
            this.stage.orientation = egret.OrientationMode.LANDSCAPE_FLIPPED;
    }
}

var isRotation = false;

var screenSize = {
    width: 1440,
    height: 900
}


var _rate: number;
var bel: number;
var bep: number;

var three_Rate: number;